﻿using ViewMaker.Core.Controls;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using ViewMaker.Core;
using System.Collections.Generic;
using System.Windows.Controls;

namespace ViewMaker.Core.Tests
{
    [TestClass()]
    public class GridViewControlTest
    {
        [TestMethod()]
        public void 標準的なGridのXAML生成()
        {
            var target = new GridViewControl { Width = 100, Item = new ViewLayoutItem { Name = "gr001", BindPath = "data" } };
            target.Item.Element = target;
            var txtItem1 = new ViewLayoutItem { Name = "txt001", Caption = "テキスト", BindPath = "textPath1" };
            txtItem1.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem1);
            var txtItem2 = new ViewLayoutItem { Name = "txt002", Caption = "テキスト", BindPath = "textPath2" };
            txtItem2.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem2);
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[0];
            var actual = TestHelper.GetFrameworkElement<Grid>(xaml);
            Assert.AreEqual("gr001", actual.Name);
            Assert.AreEqual("data", actual.GetBindingExpression(Grid.DataContextProperty).ParentBinding.Path.Path);
#if !SILVERLIGHT
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[0]));
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[2]));
#endif
            Assert.AreEqual("txt001", ((TextBox)actual.Children[1]).Name);
            Assert.AreEqual("txt002", ((TextBox)actual.Children[3]).Name);
        }

        [TestMethod()]
        public void 複数列なGridのXAML生成()
        {
            var target = new GridViewControl { Width = 100, Item = new ViewLayoutItem { Name = "gr001", BindPath = "data" } };
            target.Item.Element = target;
            target.ItemColumnSize = 4;
            target.ItemRowSize = 2;
            target.Orientation = LayoutOrientation.Horizontal;
            var txtItem1 = new ViewLayoutItem { Name = "txt001", Caption = "テキスト", BindPath = "textPath1" };
            txtItem1.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem1);
            var txtItem2 = new ViewLayoutItem { Name = "txt002", Caption = "テキスト", BindPath = "textPath2" };
            txtItem2.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem2);
            var txtItem3 = new ViewLayoutItem { Name = "txt003", Caption = "テキスト", BindPath = "textPath3" };
            txtItem3.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem3);
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[0];
            var actual = TestHelper.GetFrameworkElement<Grid>(xaml);
            Assert.AreEqual("gr001", actual.Name);
            Assert.AreEqual("data", actual.GetBindingExpression(Grid.DataContextProperty).ParentBinding.Path.Path);

#if !SILVERLIGHT
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[0]));
            Assert.AreEqual(2, Grid.GetColumn(actual.Children[2]));
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[4]));
#endif
            Assert.AreEqual("txt001", ((TextBox)actual.Children[1]).Name);
            Assert.AreEqual("txt002", ((TextBox)actual.Children[3]).Name);
        }

        [TestMethod()]
        public void 縦複数列なGridのXAML生成()
        {
            var target = new GridViewControl { Width = 100, Item = new ViewLayoutItem { Name = "gr001", BindPath = "data" } };
            target.Item.Element = target;
            target.ItemColumnSize = 2;
            target.Orientation = LayoutOrientation.Vertical;
            var txtItem1 = new ViewLayoutItem { Name = "txt001", Caption = "テキスト", BindPath = "textPath1" };
            txtItem1.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem1);
            var txtItem2 = new ViewLayoutItem { Name = "txt002", Caption = "テキスト", BindPath = "textPath2" };
            txtItem2.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem2);
            var txtItem3 = new ViewLayoutItem { Name = "txt003", Caption = "テキスト", BindPath = "textPath3" };
            txtItem3.Element = new TextBoxViewControl();
            target.Item.AddChild(txtItem3);
            var xaml = target.CreateXaml(TestHelper.CreateTestParent())[0];
            var actual = TestHelper.GetFrameworkElement<Grid>(xaml);
            Assert.AreEqual("gr001", actual.Name);
            Assert.AreEqual("data", actual.GetBindingExpression(Grid.DataContextProperty).ParentBinding.Path.Path);

#if !SILVERLIGHT
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[0]));
            Assert.AreEqual(0, Grid.GetColumn(actual.Children[2]));
            Assert.AreEqual(2, Grid.GetColumn(actual.Children[4]));
#endif
            Assert.AreEqual("txt001", ((TextBox)actual.Children[1]).Name);
            Assert.AreEqual("txt002", ((TextBox)actual.Children[3]).Name);
        }

        [TestMethod()]
        public void GridViewControlの編集画面が表示可能()
        {
            var target = new GridViewControl { Width = 100, Item = new ViewLayoutItem { Name = "gr001", BindPath = "data" } };
            target.Item.Element = target;
            target.ItemColumnSize = 4;
            target.ItemRowSize = 2;
            target.Orientation = LayoutOrientation.Horizontal;
            var baml = ViewUtil.BuildContent(target);
            Assert.IsNotNull(baml);
        }

    }
}
